<?php

/**
 * MVM_MALL 网上商店系统 商品拍卖
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-06-12 $
 * $Id: auction.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once 'include/common.inc.php';
if($action=='list'){
	$total_count = $db->counter("{$tablepre}activity","where act_type='2'");
	require_once 'include/pager.class.php';
	$page         = $page ? (int)$page : 1;
	$list_num     = $mm_limit_list;
	$rowset       = new Pager($total_count,$list_num,$page);
	$from_record  = $rowset->_offset();
	$auction_list = activity(2,$from_record,$list_num);
	if ($rewrite == 1) {
		$baseurl = str_replace('1.html','',GetBaseUrl('auction','list'));
		$exc='.html';
	} else{
		$baseurl = GetBaseUrl('auction',$action).'&page=';
	}
	require_once MVMMALL_ROOT . 'header.php';
	$page_list  = $rowset->link($baseurl, $exc);
	$navigation = make_navigation($lang['navi_auction'],$baseurl.$page.$exc);//导航
	require_once template('auction');
	footer();
}
elseif ($action=='view' && is_numeric($id)){
    $mm_brand = $cache->get_cache('brand');
    $auction  = $db->get_one("SELECT a.*,b.uid,b.goods_file1,b.goods_name,b.goods_brand FROM `{$tablepre}activity` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.act_type='2' AND a.act_id='$id'");
    $auction['photo']          = $auction['goods_file1'];
    $auction['url']            = GetBaseUrl('product',$auction['goods_id']);
    $auction['brand']          = $mm_brand[$auction['goods_brand']];
    $auction['title']          =  "<a href=\"$auction[url]\" title=\"$auction[goods_name]\">$auction[goods_name]</a>";
    $act_config                = unserialize($auction['act_config']);
    $act_config['end_price']   = currency($act_config['end_price']);
    $act_config['start_price'] = currency($act_config['start_price']);
    $act_config['amplitude']   = currency($act_config['amplitude']);
    $act_config['deposit']     = currency($act_config['deposit']);
    $auction['time']           = date($mm_time_format,$auction['end_time']);
    if($auction['is_finished']==1 || $auction['end_time']<$m_now_time){
        $end_ss                = 0;
    } else {
        $lang['activity_end']  = $lang['auction_time'];
        $end_ss                = $auction['end_time']-$m_now_time;
    }
    //拍卖记录
    $maxprice = $db->get_one("SELECT MAX(price) FROM `{$tablepre}auction_table` WHERE act_id='$id'");
	$maxprice = array_values($maxprice);
    $result   = $db->query("SELECT * FROM `{$tablepre}auction_table` WHERE act_id='$id' ORDER BY `price` DESC");
    while ($list= $db->fetch_array($result)){
    	$list['time']      = date($mm_time_format, $list['regdate']);
    	$end_ip            = substr(strrchr($list['ip'],"."),1);
    	$list['ip']        = eregi_replace(".$end_ip", ".**", $list['ip']);
    	$list['price']     = currency($list['price']);
    	if ($list['price']>=$maxprice[0]) {
    		$list['state'] = $auction['is_finished']==1?"<font color=\"#ff3300\">$lang[okhank]</font><IMG src=$imgpath/okhank.gif>":"$lang[on_top]<IMG src=$imgpath/okhank.gif>";
    	}else {
    		$list['state'] = $lang['out'];
    	}
    	$bid_list[]        = $list;
    }
    //end
    require_once MVMMALL_ROOT . 'header.php';
    $navigation = make_navigation($lang['navi_auction'],GetBaseUrl('auction','list'));//导航
    require_once template('auction_view');
    footer(); 
} 
elseif ($action=='write' && is_numeric($id)) {
	!$m_check_id && show_msg('login_please',GetBaseUrl('logging','login'));
    $auction    = $db->get_one("SELECT * FROM `{$tablepre}activity`  WHERE act_type='2' AND act_id='$id'");
    ($auction['is_finished']==1 || $auction['end_time']<$m_now_time) && show_msg('auction_end');
    $act_config = unserialize($auction['act_config']);
    $pirce      = floatval($price);
	$maxprice   = $db->get_one("SELECT MAX(price) FROM `{$tablepre}auction_table` WHERE act_id='$id'");
	$maxprice   = array_values($maxprice);
	if ($price<$maxprice[0]){
	    show_msg('price_aution');
	}elseif (($pirce-$maxprice[0])<$act_config['amplitude']){
	    show_msg('price_min');
	}elseif ($price<$act_config['start_price']){
	    show_msg('price_err');
	}
	if($price>=$act_config['end_price']){
	   $price = $act_config['end_price'];  
       $db->query("UPDATE `{$tablepre}activity` SET is_finished='1' WHERE act_type='2' AND act_id='$id'"); 
    }	
    $db->query("INSERT INTO `{$tablepre}auction_table` (act_id,userid,price,ip,regdate)
                VALUES ('$id','$m_check_id','$price','$m_user_ip','$m_now_time')");
    show_msg('success',dreferer());
} 

elseif ($action=='buy' && is_numeric($id) && is_numeric($goods_id)){
    !$m_check_id && show_msg('login_please',GetBaseUrl('logging','login'));
    $list = $db->get_one("SELECT act_id,userid,price FROM `{$tablepre}auction_table` WHERE act_id='$id' ORDER BY `price` DESC LIMIT 1 ");
    if($list['userid']!=$m_check_id){
        show_msg('auction_buy_error');
    }else{
        $old_list = $db->get_one("SELECT uid,cart_id,cart_goods_id,cart_num FROM `{$tablepre}cart_table` WHERE cart_id = '$m_check_id' AND cart_type='2' AND cart_goods_id = '$goods_id'");
        if($old_list['uid']) {
            move_page(GetBaseUrl('cart','consignee'));
        } else {
        	$add_list = $db->get_one("SELECT uid,goods_name FROM `{$tablepre}goods_table` WHERE uid = '$goods_id'");
        	!goods_stock_check($goods_id,1) && show_msg('sale_num');
        	if($add_list['uid']) {
        		$rows =array(
	                'cart_wish'      => 0,
					'cart_id'        => $m_check_id,
                    'cart_goods_id'  => $add_list['uid'],
                    'cart_sess'      => $m_check_cart,
                    'cart_subject'   => dhtmlchars($add_list['goods_name']),
                    'cart_price'     => $list['price'],
                    'cart_num'       => 1,
                    'cart_type'      => 2,
                    'register_date'  => $m_now_time
                     );
        		$db->insert("{$tablepre}cart_table",$rows);
        		$_SESSION['activity']['type'] = 2;
        		$_SESSION['activity']['id']   = $list['act_id'];
        		move_page('cart.php?action=buy');
        	}else {
        		show_msg('pass_worng');
        	}
        }
    }
} else {
    show_msg('pass_worng');
}

